<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="学院课程：">
            <el-input
              v-model="listQuery.training_name"
              class="input-width"
              placeholder="请输入学院课程"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="社区名称：">
            <el-input
              v-model="listQuery.community_name"
              class="input-width"
              placeholder="请输入社区名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号：">
            <el-input
              v-model="listQuery.phone"
              class="input-width"
              placeholder="请输入手机号"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="exportFile()"
      >导出</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          label="手机号"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column
          label="姓名"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.realname }}</template>
        </el-table-column>
        <el-table-column
          label="社区"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.community_name }}</template>
        </el-table-column>
        <el-table-column
          label="学院课程"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.training_name }}</template>
        </el-table-column>
        <el-table-column
          label="是否通过"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.is_pass_name }}</template>
        </el-table-column>
        <el-table-column
          label="分数"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.score }}</template>
        </el-table-column>
        <el-table-column
          label="考试时间"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.create_time | formatDateTime }}</template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import { getExamResultList, exportExamResultList } from '@/api/college'

import { formatDate } from '@/utils/date'
const defaultListQuery = {
  // reportType: '3',
  pageNum: 1,
  pageSize: 10
}
export default {
  name: 'AdviseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      loading: false
    }
  },
  created () {
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    getList () {
      this.listLoading = true
      const params = {
        ...this.listQuery
      }
      getExamResultList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    exportFile () {
      this.listLoading = true
      const downloadName = '考试统计结果导出'
      const params = {
        ...this.listQuery
      }
      exportExamResultList(params).then(res => {
        console.log(res)
        this.listLoading = false
        if (res) {
          const blob = new Blob([res], { type: res.type })
          const A = document.createElement('a')
          const href = window.URL.createObjectURL(blob)
          A.href = href
          const addXlsx = navigator.userAgent.indexOf('Firefox') > -1 ? '.xls' : ''
          A.download = `${downloadName || '导出记录'}-${moment(new Date()).format('YYYYMMDDHHmmssSSS')}${addXlsx}.xls`
          document.body.appendChild(A)
          A.click()
          document.body.removeChild(A)
          window.URL.revokeObjectURL(href)
          this.$message.success(`${A.download} 开始下载`)
        } else {
          this.$message.error('导出失败')
        }
      })
    }
  }
}
</script>
<style scoped>
  .input-width {
    width: 203px;
  }
</style>
